var DATE_FORMAT = 'dd-mm-yy';
var WMODE = 'product';//dev|product
var TIMEDELAY = 5000;

function noError(){return true;}
//window.onerror = noError;

$(document).ready(function(){
	
	//style select box
	$('select.custom').customStyle();
	
	//style check box
	$('input:checkbox:not([onoff]).custom').checkbox({cls:'jquery-safari-checkbox'});
	//$('input:checkbox:not([safari])').checkbox();
	$('input[onoff]:checkbox.custom').checkbox();
	//slideshow
	
	$("#accordion").accordion();
	
	$("#personal-tabs___").tabs({
		select: function(event, ui) {
			//alert(ui.index);
			$("ul.ui-tabs-nav li").removeClass("ui-tabs-next-to-selected");
			li = $("ul.ui-tabs-nav li").get(ui.index + 1)
			$(li).addClass("ui-tabs-next-to-selected");
			if(ui.index != 0){
				liFirst = $("ul.ui-tabs-nav li").get(0)
				$(liFirst).addClass("ui-tabs-first-non-selected");
			}else{
				liFirst = $("ul.ui-tabs-nav li").get(0)
				$(liFirst).removeClass("ui-tabs-first-non-selected");
			}
			
			if(ui.index >= $("#personal-tabs > ul.ui-tabs-nav li").length-2){
				liLast = $("#personal-tabs > ul.ui-tabs-nav li").get($("#personal-tabs > ul.ui-tabs-nav li").length-1);
				$(liLast).removeClass("ui-tabs-last-non-selected");
				$(liLast).addClass("ui-tabs-last-selected");	
			}else{
				liLast = $("#personal-tabs > ul.ui-tabs-nav li").get($("#personal-tabs > ul.ui-tabs-nav li").length-1);
				$(liLast).addClass("ui-tabs-last-non-selected");
			}
		}
	});
	$("#description-tabs").tabs({
		select: function(event, ui) {
			//alert(ui.index);
			$("#description-tabs > ul.ui-tabs-nav li").removeClass("ui-tabs-next-to-selected");
			li = $("#description-tabs > ul.ui-tabs-nav li").get(ui.index + 1)
			$(li).addClass("ui-tabs-next-to-selected");
			if(ui.index != 0){
				liFirst = $("#description-tabs > ul.ui-tabs-nav li").get(0)
				$(liFirst).addClass("ui-tabs-first-non-selected");
			}else{
				liFirst = $("#description-tabs > ul.ui-tabs-nav li").get(0)
				$(liFirst).removeClass("ui-tabs-first-non-selected");
			}
			
			if(ui.index >= $("#description-tabs > ul.ui-tabs-nav li").length-2){
				liLast = $("#description-tabs > ul.ui-tabs-nav li").get($("#description-tabs > ul.ui-tabs-nav li").length-1);
				$(liLast).removeClass("ui-tabs-last-non-selected");
				$(liLast).addClass("ui-tabs-last-selected");	
			}else{
				liLast = $("#description-tabs > ul.ui-tabs-nav li").get($("#description-tabs > ul.ui-tabs-nav li").length-1);
				$(liLast).addClass("ui-tabs-last-non-selected");
			}
		}
	});
	
	
	// complete tab ui
	if($("ul.ui-tabs-nav").length){
		$("ul li.ui-tabs-selected").next().addClass("ui-tabs-next-to-selected");
		liLast = $("#personal-tabs > ul.ui-tabs-nav li").get($("#personal-tabs > ul.ui-tabs-nav li").length-1);
		$(liLast).addClass("ui-tabs-last-non-selected");
		liLast1 = $("#description-tabs > ul.ui-tabs-nav li").get($("#description-tabs > ul.ui-tabs-nav li").length-1);
		$(liLast1).addClass("ui-tabs-last-non-selected");
	}
	
	$('#captcha-reload').live('click',function(){
		$('#captcha-section').html(CAPTCHA_LOADER);
		$.get(BASE_URI+'home/re_captcha',{},function(res){
			$('#captcha-section').html(res);
		});
	});
	
	$('a.bannerclicked').live('click',function(){
		$id = $(this).attr('id');
		$link = $(this).attr('href');
		$.post(BASE_URI+'home/bannerclicked',{id:$id},function(res){
			queryurl(res);
		});
		setTimeout(function(){queryurl($link);},2000);
		return false;
	});
});




var geocoder;
var map;
var marker;
var count=0;

function initialize(lat , lng) {
	geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat,lng);
    var myOptions = {
      zoom: 14,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("google_map_add_form"), myOptions);
    google.maps.event.addListener(map, 'click', function(event) {
    	mapEditor(event.latLng);
    });
    marker = new google.maps.Marker({
		  position: latlng,
		  map: map
	  });
 }

function mapEditor(location) {
    marker.setPosition(location);
	$('#contact_lat').attr('value', location.lat() ); 
	$('#contact_lng').attr('value', location.lng() ); 
}

function codeAddress(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        map.setZoom(14);
        marker.setPosition(results[0].geometry.location);
       // $('#contact_lat').val( results[0].geometry.location.lat() ); 
		//$('#contact_lng').val( results[0].geometry.location.lng() ); 
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
 }
 
 function updatedShow(divClass){
	if(divClass){
		$('.'+divClass).fadeIn();
		setTimeout(function(){$('.'+divClass).fadeOut();},TIMEDELAY);
	}else{
		$('#update-message').fadeIn();
		setTimeout(function(){$('#update-message').fadeOut();},TIMEDELAY);
	}	
 }

 function toggle(){
	$('.toggleSection').live('click',function(){
		$id = $(this).attr('rel');
		$(this).removeClass('collapse').removeClass('expand');
		if($('#'+$id).hasClass('hidden')){
			$('#'+$id).removeClass('hidden');
			$(this).addClass('collapse');
		}else{
			$('#'+$id).addClass('hidden');
			$(this).addClass('expand');
		}
	});
}